package com.marktim.beerbot;

import java.io.IOException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import com.marktim.beerbot.BeerBot.ArmDirection;
import com.marktim.beerbot.BeerBot.ArmHeight;

@Path("/bot")
public class BotService {

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/left/{left}/right/{right}")
	public String setLeft(@PathParam("left") int left, @PathParam("right") int right) throws IOException, InterruptedException {
		System.out.println("New values: LEFT " + left + ", RIGHT " + right);

		BeerBot.getInstance().resetTimer();
		BeerBot.getInstance().setCommandReceived(true);
		BeerBot.getInstance().setLeftMovement(left);
		Thread.sleep(1);
		BeerBot.getInstance().setRightMovement(right);
		
		return "{\"left\":\"" + left + "\",\"right\":\"" + right + "\"}"; 
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/info")
	public BeerBot getInfo() throws InterruptedException {
		return BeerBot.getInstance();
	}
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/arm/height/{height}")
	public String setArmHeight(@PathParam("height") String height) throws IOException, InterruptedException {
		System.out.println("New height: " + height);

		
		BeerBot.ArmHeight armHeight;
		if (height.equals("HIGH")) {
			armHeight = ArmHeight.HIGH;
		} else {
			armHeight = ArmHeight.LOW;
		}
		BeerBot.getInstance().setArmHeight(armHeight);
		
		return "{\"height\":\"" + height + "\"}"; 
	}
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/arm/direction/{dir}")
	public String setArmDirection(@PathParam("dir") String direction) throws IOException, InterruptedException {
		System.out.println("New direction: " + direction);

		
		BeerBot.ArmDirection armDirection;
		if (direction.equals("LEFT")) {
			armDirection = ArmDirection.LEFT;
		} else if (direction.equals("CENTER")){
			armDirection = ArmDirection.CENTER;
		} else {
			armDirection = ArmDirection.RIGHT;
		}
		BeerBot.getInstance().setArmDirection(armDirection);
		
		return "{\"direction\":\"" + direction + "\"}"; 
	}

}
